package com.foodorder;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * 餐饮点餐系统主启动类
 * 
 * @author FoodOrder Team
 * @version 1.0.0
 */
@Slf4j
@SpringBootApplication
@EnableScheduling
public class FoodOrderApplication {

    private final JdbcTemplate jdbcTemplate;
    private final PasswordEncoder passwordEncoder;

    public FoodOrderApplication(JdbcTemplate jdbcTemplate, PasswordEncoder passwordEncoder) {
        this.jdbcTemplate = jdbcTemplate;
        this.passwordEncoder = passwordEncoder;
    }

    public static void main(String[] args) {
        SpringApplication.run(FoodOrderApplication.class, args);
        System.out.println("=================================");
        System.out.println("餐饮点餐系统后端服务启动成功！");
        System.out.println("API地址: http://localhost:8080/api");
        System.out.println("H2数据库控制台: http://localhost:8080/api/h2-console");
        System.out.println("=================================");
    }

    @EventListener(ApplicationReadyEvent.class)
    public void fixAdminPassword() {
        try {
            // 固定使用123456作为密码
            String password = "123456";
            String encodedPassword = "$2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy";
            
            System.out.println("=========== 检查管理员账号 ===========");
            Integer count = jdbcTemplate.queryForObject(
                "SELECT COUNT(*) FROM admins WHERE username = ?", 
                Integer.class, 
                "admin"
            );
            
            if (count != null && count > 0) {
                System.out.println("发现管理员账号，尝试更新密码...");
                int updated = jdbcTemplate.update(
                    "UPDATE admins SET password = ? WHERE username = ?",
                    encodedPassword, "admin"
                );
                System.out.println("管理员密码已重置: " + updated + " 条记录更新");
                System.out.println("用户名: admin");
                System.out.println("密码: " + password);
            } else {
                System.out.println("未发现管理员账号，尝试创建新账号...");
                int inserted = jdbcTemplate.update(
                    "INSERT INTO admins (username, password, name, phone, email, avatar, role, status, permissions, created_at, updated_at) " +
                    "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)",
                    "admin", encodedPassword, "系统管理员", "13800138000", "admin@foodorder.com", 
                    "/images/avatars/admin.jpg", "SUPER_ADMIN", "ACTIVE", "ALL"
                );
                System.out.println("新管理员账号创建: " + inserted + " 条记录插入");
                System.out.println("用户名: admin");
                System.out.println("密码: " + password);
            }
            System.out.println("=========== 管理员账号修复完成 ===========");
        } catch (Exception e) {
            System.err.println("修复管理员密码时出错: " + e.getMessage());
            e.printStackTrace();
        }
    }
} 